<?php

namespace app\admin\controller;

use app\admin\model\Oauth;
use app\admin\model\TieBaComment;
use app\common\controller\Send;
use app\common\enums\StatusEnum;
use app\admin\model\TieBa AS TieModel;
use app\admin\model\TieBaType;

class TieBa extends Base
{
    use Send;

    public function common()
    {
        $search = input('');
        $oauthId = isset($search['oauth_id']) ? $search['oauth_id'] : 0;
        $typeId = isset($search['type_id']) ? $search['type_id'] : -1;
        $checkStatus = isset($search['check_status']) ? $search['check_status'] : -1;
        $search = [
            'oauth_id' => $oauthId,
            'type_id' => $typeId,
            'check_status' => $checkStatus
        ];
        $where = [];
        $oauth = [
            'id' => 0,
            'nickname' => '',
            'phone' => '',
        ];
        if ($search['oauth_id'] > 0) {
            $oauth = Oauth::getOne($search['oauth_id']);
            $where[] = ['oauth_id', '=', $search['oauth_id']];
        }
        if ($search['type_id'] != null && $search['type_id'] != -1) {
            $where[] = ['type_id', '=', $search['type_id']];
        }

        if ($search['check_status'] != null && $search['check_status'] != -1) {
            $where[] = ['check_status', '=', $search['check_status']];
        }

        $model = TieModel::getList($where);
        return [
            'title' => '帖子列表',
            'noOauth_id' => 0,
            'detailType' => 1,
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'type' => TieBaType::getColumn(),
            'checkStatus' => TieModel::attributes('check_status'),
            'oauth' => $oauth
        ];
    }

    /**
     * 列表
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data = $this->common();
        $data['cancelUrl'] = url('index');
        return view('index', $data);
    }

    public function mine()
    {
        $data = $this->common();
        $data['title'] = '我的帖子';
        $data['noOauth_id'] = 1;
        $data['detailType'] = 2;
        $data['cancelUrl'] = url('mine', ['oauth_id' => $data['search']['oauth_id']]);
        return view('index', $data);
    }

    /**
     * 详情
     * @param $id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id)
    {
        $model = TieModel::getOne($id);
        
        $detailType = input('detailType');
        switch ($detailType) {
            case 2:
                $title = '我的帖子';
                $url = url('mine', ['oauth_id' => $model['oauth_id']]);
                break;
            case 3:
                $title = '收藏帖子';
                $url = url('tie_ba_collect/index', ['oauth_id' => $model['oauth_id']]);
                break;
            default:
                $title = '帖子';
                $url = url('index');
                break;
        }
        $fileList = TieModel::getFileByTieId($id);
       

        return view('detail', [
            'model' => $model,
            'checkStatus' => TieModel::attributes('check_status'),
            'title' => $title,
            'url' => $url
        ]);
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        TieModel::deleteOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 审核状态
     * @param $id
     * @return array
     */
    public function form($id)
    {
        $status = input('check_status');
        TieModel::checkOne($id, $status);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function top($id)
    {
        TieModel::topOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    public function deletes()
    {
        $ids = input('ids');
        $deleteType = input('deleteType');
        $oauthId = input('oauth_id');
        // 删除数据
        TieModel::deleteAll($ids);
        switch ($deleteType) {
            case 2:
                $url = url('mine', ['oauth_id' => $oauthId]);
                break;
            default:
                $url = url('index');
                break;
        }
        return self::sendResult(self::$okCode, [
            'url' => $url,
        ]);
    }
}
